package tests;

import java.util.regex.Pattern;
import java.util.concurrent.TimeUnit;
import org.testng.*;
import org.testng.annotations.*;
//import static org.junit.Assert.*;
//import org.junit.Assert;
//import static junit.framework.Assert.*;
//import junit.framework.Assert;
import static org.testng.Assert.*;
import org.testng.Assert;
import static org.hamcrest.CoreMatchers.*;
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;



public class AddMovie extends tests.supers.TestBaseOld {
  private boolean acceptNextAlert = true;
  private StringBuffer verificationErrors = new StringBuffer();

  @Test
  public void testaddMovie() throws Exception {

	String ts = new SimpleDateFormat("ddMMyyHHmm").format(new Timestamp(new Date().getTime()));
    driver.get(baseUrl + "/php4dvd/");
    driver.findElement(By.id("username")).clear();
    driver.findElement(By.id("username")).sendKeys("admin");
    driver.findElement(By.name("password")).clear();
    driver.findElement(By.name("password")).sendKeys("admin");
    driver.findElement(By.name("submit")).click();
    driver.findElement(By.cssSelector("img[alt=\"Add movie\"]")).click();
    driver.findElement(By.name("imdbid")).clear();
    driver.findElement(By.name("imdbid")).sendKeys(ts);
    driver.findElement(By.name("aka")).clear();
    driver.findElement(By.name("aka")).sendKeys("The slow movie");
    driver.findElement(By.name("duration")).clear();
    driver.findElement(By.name("duration")).sendKeys("1hr50min");
    driver.findElement(By.name("rating")).clear();
    driver.findElement(By.name("rating")).sendKeys("high");
    driver.findElement(By.id("own_no")).click();
    driver.findElement(By.id("seen_no")).click();
    driver.findElement(By.id("loaned_yes")).click();
    driver.findElement(By.name("loanname")).clear();
    driver.findElement(By.name("loanname")).sendKeys("patron");
    driver.findElement(By.name("notes")).clear();
    driver.findElement(By.name("notes")).sendKeys("test note");
    driver.findElement(By.name("taglines")).clear();
    driver.findElement(By.name("taglines")).sendKeys("tag1movie");
    driver.findElement(By.id("text_languages_0")).clear();
    driver.findElement(By.id("text_languages_0")).sendKeys("english, italian");
    driver.findElement(By.name("subtitles")).clear();
    driver.findElement(By.name("subtitles")).sendKeys("english, corian, chineese");
    driver.findElement(By.name("audio")).clear();
    driver.findElement(By.name("audio")).sendKeys("no");
    driver.findElement(By.name("video")).clear();
    driver.findElement(By.name("video")).sendKeys("yes");
    driver.findElement(By.name("country")).clear();
    driver.findElement(By.name("country")).sendKeys("USA");
    driver.findElement(By.name("genres")).clear();
    driver.findElement(By.name("genres")).sendKeys("musical");
    driver.findElement(By.name("music")).clear();
    driver.findElement(By.name("music")).sendKeys("John Williams");
    driver.findElement(By.id("submit")).click();
    driver.findElement(By.name("duration")).clear();
    driver.findElement(By.name("duration")).sendKeys("110");
    driver.findElement(By.name("rating")).clear();
    driver.findElement(By.name("rating")).sendKeys("10");
    driver.findElement(By.cssSelector("img[alt=\"Save\"]")).click();
    driver.findElement(By.id("submit")).click();
    driver.findElement(By.name("year")).clear();
    driver.findElement(By.name("year")).sendKeys("1967");
    driver.findElement(By.cssSelector("img[alt=\"Save\"]")).click();
    driver.findElement(By.id("submit")).click();
    driver.findElement(By.name("name")).clear();
    driver.findElement(By.name("name")).sendKeys("Movie"+ts);
    driver.findElement(By.cssSelector("img[alt=\"Save\"]")).click();
   // driver.findElement(By.id("submit")).click();
    driver.findElement(By.linkText("Home")).click();
    driver.findElement(By.id("q")).clear();
    driver.findElement(By.id("q")).sendKeys("Movie"+ts + Keys.RETURN);

    try {
    	String elementText = driver.findElement(By.cssSelector("div.title")).getText();

    	Assert.assertTrue(elementText.contains("Movie"+ts));
    	System.out.println("Movie was added successfully");
    	}
    	catch (Exception e) {
    	//System.err.println("The movie wasn't added: "+ "Movie"+ts);
    		driver.findElement(By.linkText("Log out")).click();
    	    assertTrue(closeAlertAndGetItsText().matches("^Are you sure you want to log out[\\s\\S]$"));
    	Assert.fail("The movie wasn't added: "+ "Movie"+ts);
    	}

    driver.findElement(By.linkText("Log out")).click();
    assertTrue(closeAlertAndGetItsText().matches("^Are you sure you want to log out[\\s\\S]$"));
  }

  private boolean isElementPresent(By by) {
    try {
      driver.findElement(by);
      return true;
    } catch (NoSuchElementException e) {
      return false;
    }
  }

  private String closeAlertAndGetItsText() {
    try {
      Alert alert = driver.switchTo().alert();
      String alertText = alert.getText();
      if (acceptNextAlert) {
        alert.accept();
      } else {
        alert.dismiss();
      }
      return alertText;
    } finally {
      acceptNextAlert = true;
    }
  }
}

